/**************************************************************************************
*
*	Copyright (C) 2010 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	DX9Mapping.cpp
*
*	Comments	-	See DX9Mapping.h
*
**************************************************************************************/

#ifdef __PSX_WINDOWS_WINDOW

#include "../Include/DX9Mapping.h"
#include <dxerr.h>

#pragma comment( lib, "dxerr.lib" )


namespace Pulse
{
	D3DDECLTYPE DX9Mapping::ToDeclarationType( const EVertexElementUsage::Type usage )
	{
		switch( usage )
		{
		case EVertexElementUsage::POSITION:
		case EVertexElementUsage::NORMAL:
		case EVertexElementUsage::BINORMAL:
		case EVertexElementUsage::TANGENT:
			return D3DDECLTYPE_FLOAT3;
		case EVertexElementUsage::TEXCOORD:
			return D3DDECLTYPE_FLOAT2;
		case EVertexElementUsage::COLOR:
			return D3DDECLTYPE_D3DCOLOR;
		case EVertexElementUsage::FOG:
		case EVertexElementUsage::DEPTH:
		case EVertexElementUsage::SAMPLE:
		case EVertexElementUsage::BLEND_WEIGHTS:
		case EVertexElementUsage::BLEND_INDICES:
			{ }
		}

		//PSX_PushError( "Not implemented yet..." );

		return D3DDECLTYPE_FLOAT3;
	}

	D3DDECLUSAGE DX9Mapping::ToDeclarationUsage( const EVertexElementUsage::Type usage )
	{
		switch( usage )
		{
			case EVertexElementUsage::POSITION:
				return D3DDECLUSAGE_POSITION;
			case EVertexElementUsage::NORMAL:
				return D3DDECLUSAGE_NORMAL;
			case EVertexElementUsage::BINORMAL:
				return D3DDECLUSAGE_BINORMAL;
			case EVertexElementUsage::TANGENT:
				return D3DDECLUSAGE_TANGENT;
			case EVertexElementUsage::TEXCOORD:
				return D3DDECLUSAGE_TEXCOORD;
			case EVertexElementUsage::COLOR:
				return D3DDECLUSAGE_COLOR;
			case EVertexElementUsage::FOG:
				return D3DDECLUSAGE_FOG;
			case EVertexElementUsage::DEPTH:
				return D3DDECLUSAGE_DEPTH;
			case EVertexElementUsage::SAMPLE:
				return D3DDECLUSAGE_SAMPLE;
			case EVertexElementUsage::BLEND_WEIGHTS:
				return D3DDECLUSAGE_BLENDWEIGHT;
			case EVertexElementUsage::BLEND_INDICES:
				return D3DDECLUSAGE_BLENDINDICES;
		}

		return D3DDECLUSAGE_POSITION;
	}

	D3DVERTEXELEMENT9 DX9Mapping::ToVertexElement( const VertexElement *pElement )
	{
		D3DVERTEXELEMENT9 ret;

		ret.Stream = pElement->stream;
		ret.Offset = pElement->offset;
		ret.Type = DX9Mapping::ToDeclarationType( pElement->usage );
		ret.Method = D3DDECLMETHOD_DEFAULT;
		ret.Usage = DX9Mapping::ToDeclarationUsage( pElement->usage );
		ret.UsageIndex = pElement->usageIndex;

		return ret;
	}

	const CHAR * DX9Mapping::GetDXErrorDescription( HRESULT hr )
	{
		return DXGetErrorDescription( hr );
	}

	D3DFORMAT DX9Mapping::ToFormat( ETextureFormat::Type format )
	{
		switch( format )
		{
		case ETextureFormat::A8R8G8B8:		
			return D3DFMT_A8R8G8B8;
		case ETextureFormat::A2R10G10B10:	
			return D3DFMT_A2R10G10B10;
		case ETextureFormat::R8G8B8:
			return D3DFMT_X8R8G8B8;
		case ETextureFormat::A1R5G5B5:		
			return D3DFMT_A1R5G5B5;
		case ETextureFormat::R5G5B5:			
			return D3DFMT_X1R5G5B5;
		case ETextureFormat::R5G6B5:			
			return D3DFMT_R5G6B5;
		case ETextureFormat::G16R16F:		
			return D3DFMT_G16R16F;
		case ETextureFormat::A16B16G16R16F:	
			return D3DFMT_A16B16G16R16F;
		case ETextureFormat::R16F:			
			return D3DFMT_R16F;
		case ETextureFormat::G32R32F:		
			return D3DFMT_G32R32F;
		case ETextureFormat::R32F:			
			return D3DFMT_R32F;
		case ETextureFormat::A32B32G32R32F:	
			return D3DFMT_A32B32G32R32F;
		case ETextureFormat::A8:	
			return D3DFMT_A8;
		case ETextureFormat::DEFAULT:
		case ETextureFormat::UNKNOWN:
		default:
			return D3DFMT_UNKNOWN;
		}
	}


}

#endif /* __PSX_WINDOWS_WINDOW */
